extends Control

@onready var grid = $BackgroundLayer/Background/LeftPanel/VBoxContainer/GridContainer
@onready var item_details = $BackgroundLayer/Background/ItemDetails
@onready var item_name_label = $BackgroundLayer/Background/ItemDetails/VBoxContainer/ItemName
@onready var item_type_label = $BackgroundLayer/Background/ItemDetails/VBoxContainer/ItemType
@onready var item_description_label = $BackgroundLayer/Background/ItemDetails/VBoxContainer/ItemDescription
@onready var damage_label = $BackgroundLayer/Background/ItemDetails/VBoxContainer/Stats/Damage
@onready var defense_label = $BackgroundLayer/Background/ItemDetails/VBoxContainer/Stats/Defense
@onready var background = $BackgroundLayer/Background

var inventory: Inventory
var slot_scene = preload("res://scenes/inventory_slot_ui.tscn")
var initial_background_width: float
var details_width = 200
var current_item: Item = null  # 记录当前显示的物品

func _ready():
	add_to_group("inventory_ui")
	print("InventoryUI ready")
	
	# 确保背包和背景层都是隐藏的
	$BackgroundLayer.visible = false
	visible = false
	
	# 等待一帧确保所有节点都准备好
	await get_tree().process_frame
	
	inventory = get_node("/root/InventoryManager").inventory
	if inventory:
		print("Got inventory reference")
		inventory.inventory_changed.connect(_on_inventory_changed)
		_setup_slots()
		initial_background_width = 600  # 设置固定的初始宽度
		_hide_details(false)
	else:
		print("Failed to get inventory reference")

func _setup_slots():
	# 确保 grid 节点存在
	if not grid:
		print("Grid node not found!")
		return
		
	for slot in inventory.slots:
		var slot_ui = slot_scene.instantiate()
		grid.add_child(slot_ui)
		slot_ui.set_slot(slot)
		slot_ui.item_clicked.connect(_on_item_clicked)

func _on_inventory_changed():
	for i in grid.get_child_count():
		var slot_ui = grid.get_child(i)
		slot_ui.update_slot(inventory.slots[i])

func _hide_details(animate: bool = true):
	if animate:
		var tween = create_tween()
		tween.set_ease(Tween.EASE_OUT)
		tween.set_trans(Tween.TRANS_BACK)
		
		# 让详情面板淡出并向右滑动
		tween.tween_property(item_details, "modulate:a", 0.0, 0.2)
		tween.parallel().tween_property(item_details, "position:x", item_details.position.x + 80, 0.2)  # 增加偏移量
		
		tween.tween_callback(func(): 
			item_details.visible = false
			current_item = null
			# 重置位置以备下次显示
			item_details.position.x = item_details.position.x - 80  # 对应增加的偏移量
		)
	else:
		item_details.visible = false
		item_details.modulate.a = 0
		current_item = null

func _show_details(animate: bool = true):
	item_details.visible = true
	if animate:
		item_details.modulate.a = 0
		item_details.position.x = item_details.position.x + 80  # 增加偏移量
		
		var tween = create_tween()
		tween.set_ease(Tween.EASE_OUT)
		tween.set_trans(Tween.TRANS_BACK)
		
		# 让详情面板淡入并从右滑入
		tween.tween_property(item_details, "modulate:a", 1.0, 0.2)
		tween.parallel().tween_property(item_details, "position:x", item_details.position.x - 80, 0.2)  # 对应增加的偏移量
	else:
		item_details.modulate.a = 1

func _on_item_clicked(item: Item):
	if current_item == item:  # 如果点击的是当前显示的物品
		_hide_details()
	else:
		_show_details()
		item_name_label.text = item.name
		item_type_label.text = _get_type_text(item.type)
		item_description_label.text = item.description
		damage_label.text = "伤害: " + str(item.damage)
		defense_label.text = "防御: " + str(item.defense)
		current_item = item  # 记录当前显示的物品

func _get_type_text(type: Item.ItemType) -> String:
	match type:
		Item.ItemType.CONSUMABLE:
			return "消耗品"
		Item.ItemType.EQUIPMENT:
			return "装备"
		Item.ItemType.MATERIAL:
			return "材料"
		Item.ItemType.QUEST:
			return "任务物品"
		Item.ItemType.KEY:
			return "关键物品"
		_:
			return "未知"

func _input(event):
	if event.is_action_pressed("inventory"):
		$BackgroundLayer.visible = !$BackgroundLayer.visible
		if $BackgroundLayer.visible:
			_hide_details(false)  # 打开背包时隐藏详情面板
			# 只暂停部分节点，不暂停战斗相关节点
			get_tree().paused = false
			# 设置背包UI的process_mode，使其在游戏运行时也能处理输入
			process_mode = Node.PROCESS_MODE_ALWAYS
		else:
			# 关闭背包时恢复正常
			process_mode = Node.PROCESS_MODE_INHERIT
			get_tree().paused = false
